﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DO.Workflows;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace DAL.Workflows
{
    public class RunDALC
    {
        public static void AddRun(Run run)
        {
            Database db = DatabaseFactory.CreateDatabase("Workflow");
            db.ExecuteNonQuery("RunInsert",
                               run.RunID,
                               run.WorkflowID,
                               run.SubmitTime,
                               run.ReceiveTime,
                               run.StartTime,
                               run.FinishTime,
                               Run.SerializeVariables(run.Variables));
        }

        public static void SaveRun(Run run)
        {
            Database db = DatabaseFactory.CreateDatabase("Workflow");
            db.ExecuteNonQuery("RunUpdate",
                               run.RunID,
                               run.WorkflowID,
                               run.SubmitTime,
                               run.ReceiveTime,
                               run.StartTime,
                               run.FinishTime,
                               Run.SerializeVariables(run.Variables));
        }

        public static List<Run> GetWorkflowRuns(Guid workflowId)
        {
            List<Run> runs=new List<Run>();
            Database db = DatabaseFactory.CreateDatabase("Workflow");
            IDataReader reader = db.ExecuteReader("RunGetByWorkflow", workflowId);
            while(reader.Read())
            {
                Guid runID=new Guid(reader["RunID"].ToString());
                Guid workflowID=new Guid(reader["WorkflowID"].ToString());
                DateTime? submitTime = reader["SubmitTime"] == DBNull.Value
                                           ? new DateTime?()
                                           : DateTime.Parse(reader["SubmitTime"].ToString());
                DateTime? receiveTime = reader["ReceiveTime"] == DBNull.Value
                                           ? new DateTime?()
                                           : DateTime.Parse(reader["ReceiveTime"].ToString());
                DateTime? startTime = reader["StartTime"] == DBNull.Value
                                           ? new DateTime?()
                                           : DateTime.Parse(reader["StartTime"].ToString());
                DateTime? finishTime = reader["FinishTime"] == DBNull.Value
                                           ? new DateTime?()
                                           : DateTime.Parse(reader["FinishTime"].ToString());
                string xml = reader["Vars"].ToString();
                List<GlobalVariable> vars = Run.InstantiateVariables(xml);
                Run run = new Run
                              {
                                  RunID = runID,
                                  WorkflowID = workflowID,
                                  SubmitTime = submitTime,
                                  ReceiveTime = receiveTime,
                                  StartTime = startTime,
                                  FinishTime = finishTime,
                                  Variables = vars
                              };
                runs.Add(run);
            }
            reader.Close();
            return runs;
        }

        public static Run GetRunByJob(Guid jobID)
        {
            Run run = null;
            Database db = DatabaseFactory.CreateDatabase("Workflow");
            IDataReader reader = db.ExecuteReader("RunGetByJob", jobID);
            if (reader.Read())
            {
                Guid runID = new Guid(reader["RunID"].ToString());
                Guid workflowID = new Guid(reader["WorkflowID"].ToString());
                DateTime? submitTime = reader["SubmitTime"] == DBNull.Value
                                           ? new DateTime?()
                                           : DateTime.Parse(reader["SubmitTime"].ToString());
                DateTime? receiveTime = reader["ReceiveTime"] == DBNull.Value
                                           ? new DateTime?()
                                           : DateTime.Parse(reader["ReceiveTime"].ToString());
                DateTime? startTime = reader["StartTime"] == DBNull.Value
                                           ? new DateTime?()
                                           : DateTime.Parse(reader["StartTime"].ToString());
                DateTime? finishTime = reader["FinishTime"] == DBNull.Value
                                           ? new DateTime?()
                                           : DateTime.Parse(reader["FinishTime"].ToString());
                string xml = reader["Vars"].ToString();
                List<GlobalVariable> vars = Run.InstantiateVariables(xml);
                run = new Run
                {
                    RunID = runID,
                    WorkflowID = workflowID,
                    SubmitTime = submitTime,
                    ReceiveTime = receiveTime,
                    StartTime = startTime,
                    FinishTime = finishTime,
                    Variables = vars
                };
            }
            reader.Close();

            return run;
        }
    }
}
